<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card shadow="always">
      <el-row>
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="ss()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="7">
          <el-button type="primary" @click="dialogFormVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 添加对话框 -->
      <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addusers">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 修改状态 -->
      <el-dialog
        title="修改用户"
        :visible.sync="dialogFormVisible1"
      >
        <el-form :model="ruleForm1" :rules="rules" ref="ruleForm1">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm1.username"  :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm1.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleForm1.mobile"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="do_update()">修改</el-button>
        </div>
      </el-dialog>
      <!-- 分配角色 -->
      <el-dialog title="分配角色" :visible.sync="dialogFormVisible2">
       
          <p>当前用户：{{form3.username }}</p>
          <p>当前角色：{{ form3.role_name }}</p>
          <!-- element 选择器 -->
          <p> 分配新角色：
            <el-select v-model="roleList.id" placeholder="请选择">
              <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </p>
      
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addrole(roleList.id)"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!--  表格 -->
      <el-table :data="tableData" border style="width: 100% hegiht:20px">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态">
          <!-- 开关状态 -->
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="zt(scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="290">
          <template slot-scope="scope">
            <!-- 删除 修改按钮 -->
            <el-button
              @click="del(scope.row.id)"
              type="danger"
              icon="el-icon-delete"
              circle
              class="danger"
            ></el-button>
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="update(scope.row)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-star-off"
              circle
              @click="role(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
// 在这里引入api中封装接口
import {
  userList,
  addUser,
  deluser,
  changestatus,
  edituser,
  roleuser,
  changrole
} from "../../utils/api";
export default {
  data() {
    return {
      queryInfo: {
        query: "", //当前页数
        pagenum: 1, // 每页显示多少数据
        pagesize: 5,
      },
      total: 0,
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false, // 分配角色对话框
      tableData: [],
      ruleForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      ruleForm1: {},
      // 分配角色表单
      form3:{},
      roleList:[],
      formLabelWidth: "120px",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "长度在 11个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getlist();
  },
  // 将数据渲染到页面
  methods: {
    async getlist() {
      var res = await userList(this.queryInfo);
      console.log(118, res);
      if (res.data.meta.status !== 200) {
        return this.$message.error(res.data.meta.msg);
      }
      this.$message.success(res.data.meta.msg);
      this.tableData = res.data.data.users;
      this.total = res.data.data.total;
    },
    // 点击确定 添加用户信息

    addusers() {
      this.$refs.ruleForm.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        var res = await addUser(this.ruleForm);
        if (res.data.status === 201) {
          this.$message.success(res.data.meta.msg);
          // 重新获取用户列表数据
        }
        this.getlist();
        // 隐藏添加用户的对话框
        this.dialogFormVisible = false;
      });
    },
    // 删除
    del(id) {
      this.$confirm("此操作将删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          var res = await deluser(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getlist();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 搜索
    ss() {
      this.getlist();
    },

    // 修改状态
    async zt(obj) {
      var reg = {
        id: obj.id,
        mg_state: obj.mg_state,
      };
      var res = await changestatus(obj);
      console.log(res);
    },

    // 点击修改按钮 显示对话框
    update(obj) {
      console.log("1231", obj);
      this.dialogFormVisible1 = true;
      this.ruleForm1 = obj;
    },

    // 执行修改
    async do_update() {
      var res = await edituser(this.ruleForm1.id, this.ruleForm1);
      console.log("256", res);
      // 关闭修改对话框
      this.dialogFormVisible1 = false;
      // 调用
      this.getlist();
      //修改成功弹出成功提示框
      this.$message.success("更新用户信息成功！");
    },

    // 分配角色
    async role(reg) {
     console.log(11111,reg);
      this.form3= reg;
      console.log( "296",reg);
      var res = await roleuser()
      // // console.log("307",res);
      this.roleList = res.data.data
       this.dialogFormVisible2 = true;
      
    },

    // 添加用户角色
  async  addrole(id){
       var res = await changrole(this.form3,id)
      //  console.log("320",res);
      if(res.data.meta.status==200){
        this.$message.success(res.data.meta.msg)
      }
      this.dialogFormVisible2 = false;
      this.getlist()
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getlist();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-card {
  margin-top: 20px;
}
.el-table {
  margin-top: 30px;
}
</style>